allclear();

M = 100;
N = 200;

% Number of iteration;
iter = 5;

% Number of frame (N bits per frame)
frame = 5;

% Make the LDPC matrix
H = makeLdpc(M, N, 1, 1, 3);

ber = 0;

% Make random data (0/1)
% The total length of data is frame*M
dSource = round(rand(M, frame));

for j = 1:frame

  % Encoding message
  dSource(:, j);
  [c, newH] = makeParityChk(dSource(:, j), H, 2);
  u = [c; dSource(:, j)];
  
   a = ppm_mod(4, u);
   b = apply_lognormal(1, a);
   c = ppm_demod(4, b);

  % Dummy BPSK modulation. The input to the LDPC decoder is the
  % log-likelihood ratio.
  %bpskMod = 2*(c') - 1;
  tx = c';% + sqrt(N0/2)*randn(size(c'));

  % Decoding (select decoding method)
  % vhat = decodeProbDomain(tx, newH, N0, iter);
  %vhat = decodeLogDomain(tx, newH, N0, iter);
  vhat = decodeLogDomainSimple(tx, newH, iter);
  %vhat = decodeBitFlip(tx, newH, iter);

  % Get bit error rate (for brevity, BER calculation includes parity bits)
  [num, rat] = biterr(vhat', u);
  ber = (ber + rat);

end

% Get average of BER
ber = ber/frame